package com.example.travelhelper.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.travelhelper.mapper.CustomerSeMapper;
import com.example.travelhelper.pojo.CustomerService;
import com.example.travelhelper.service.CustomerSeService;
import com.example.travelhelper.utils.RandomUtil;
import com.example.travelhelper.utils.ResponseData;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * @Author: zq
 * @Description: TODO
 * @Date: 2024/4/9 11:56
 * @Version: 1.8
 */
@Service
public class CustomerSeServiceImpl implements CustomerSeService {
    @Autowired
    private CustomerSeMapper customerSeMapper;

    //查询客服电话
    @Override
    public boolean selectByPhone(String csTelephone) {
        if (customerSeMapper.selectByPhone(csTelephone)!=null){
            return true;
        }
        return false;
    }

/*    //通过id删除账号
    @Override
    public void deleteById(String id) {
        customerSeMapper.deleteById(id);
    }*/

    //通过id删除账号
    @Override
    public ResponseData deleteById(String id) {
        int i = customerSeMapper.deleteById(id);
        System.out.println(i);
        if (i!=0){
            return ResponseData.success("删除成功！");
        }else {
            return ResponseData.fail("删除失败！");
        }
    }

    //通过id查询账号
    @Override
    public CustomerService getById(String id) {
        CustomerService customerService = customerSeMapper.selectById(id);
        return customerService;
    }

    //增加客服
    @Override
    public void addCustomerSe(String csUsername, String csTelephone) {
        CustomerService customerService = new CustomerService();
        //UUid随机生成唯一id
        String csId = UUID.randomUUID().toString();
        customerService.setCsId(csId);
        customerService.setCsUsername(csUsername);
        customerService.setCsTelephone(csTelephone);


        customerSeMapper.addCustomerSe(customerService);
    }

/*    //修改账号
    @Override
    public void update(CustomerService customerService) {
        // 使用 LambdaUpdateWrapper 构造更新条件
        LambdaUpdateWrapper<CustomerService> wrapper = new LambdaUpdateWrapper<>();

        // 使用 Java 8 的 Optional 类型和方法引用，只设置属性不为空的字段
        Optional.ofNullable(customerService.getCsUsername()).ifPresent(username -> wrapper.set(CustomerService::getCsUsername, username));
        Optional.ofNullable(customerService.getCsTelephone()).ifPresent(telephone -> wrapper.set(CustomerService::getCsTelephone, telephone));

        // 设置更新条件
        wrapper.eq(CustomerService::getCsId, customerService.getCsId());
        // 执行更新操作
        customerSeMapper.update(null, wrapper);
    }*/

    //修改账号
    @Override
    public ResponseData update(CustomerService customerService) {
        // 使用 LambdaUpdateWrapper 构造更新条件
        LambdaUpdateWrapper<CustomerService> wrapper = new LambdaUpdateWrapper<>();

        // 使用 Java 8 的 Optional 类型和方法引用，只设置属性不为空的字段
        Optional.ofNullable(customerService.getCsUsername()).ifPresent(username -> wrapper.set(CustomerService::getCsUsername, username));
        Optional.ofNullable(customerService.getCsTelephone()).ifPresent(telephone -> wrapper.set(CustomerService::getCsTelephone, telephone));

        // 设置更新条件
        wrapper.eq(CustomerService::getCsId, customerService.getCsId());
        // 执行更新操作
        int flag = customerSeMapper.update(null, wrapper);
        System.out.println(flag);
        if (flag!=0){
            return ResponseData.success("更新成功！");
        }else {
            return ResponseData.fail("更新失败！");
        }
    }

    @Override
    public List<CustomerService> list() {
        List<CustomerService> customerServices = customerSeMapper.selectList(null);
        return customerServices;
    }

    @Override
    public ResponseData getbyState() {
        List<CustomerService> customerServices = customerSeMapper.listAllByCsState();

        if(customerServices.size()<1) {
            return ResponseData.fail("今天没人上班");
        }
        CustomerService customerService = customerServices.get(RandomUtil.randomOid(customerServices.size()));

        return ResponseData.success("成功查询",customerService);
    }

    //判断是不是正在上班
    @Override
    public ResponseData isstate(String phone) {
        try {
            System.out.println(phone);
            CustomerService customerService = customerSeMapper.selectByPhoneandState(phone);
            if(customerService==null){
                return ResponseData.fail("此客服没有上班");
            }
        }catch (Exception e){
            return ResponseData.fail("此客服没有上班");
        }


        return  ResponseData.success("此客服正在上班");

    }

    @Override
    public ResponseData updateState(CustomerService customerService) {
        try {
            int i = customerSeMapper.updateCsState(customerService.getCsState()==0?1:0, customerService.getCsId());
            if(i==1)
                return ResponseData.success("修改成功");

        }catch (Exception e){
            return ResponseData.fail("修改失败");
        }

        return ResponseData.fail("修改失败");

    }
}
